package ynu.lcy.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*;
import ynu.lcy.dto.cart.UpdateCartRequestDTO;
import ynu.lcy.elememicroservicecommon.utils.BaseResponse;
import ynu.lcy.elememicroservicecommon.utils.ResultUtils;
import ynu.lcy.elememicroserviceserviceclient.service.BusinessFeignClient;
import ynu.lcy.entity.Cart;
import ynu.lcy.entity.CartFoodItem;
import ynu.lcy.service.CartService;
import ynu.lcy.vo.BusinessVO;
import ynu.lcy.vo.CartItemVO;
import ynu.lcy.vo.CartVO;
import ynu.lcy.vo.FoodVO;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@CrossOrigin
@RequestMapping("/cart")
@Api(tags="购物车接口")
public class CartController {
    @Resource
    private CartService service;

    @Resource
    private BusinessFeignClient businessFeignClient;

    @Resource
    private ModelMapper modelMapper;

    /**
     * 获取该商家购物车
     * @param id 商家id
     * @param request 请求
     * @return
     * */
    @GetMapping("/getByBusiness/{id}")
    @ApiOperation("获取该商家购物车")
    public BaseResponse<CartVO> getByBusinessAndUser(@PathVariable("id") Long id, HttpServletRequest request){
        Cart cart = service.getCart(id, request);
        if (Objects.isNull(cart)) {
            return  ResultUtils.success(null);
        }
        CartVO vo = ObjectToVO(cart);
        return  ResultUtils.success(vo);
    }

    /**
     * 获取全部购物车
     *  @param request 请求
     * @return
     * */
    @GetMapping("/list")
    @ApiOperation("获取用户全部购物车")
    public BaseResponse<List<CartVO>> list(HttpServletRequest request){
        List<Cart> list = service.list(request);
        if (Objects.isNull(list)|| list.size() == 0){
            return  ResultUtils.success(null);
        }
        List<CartVO> vos = new ArrayList<>();
        list.forEach(cart -> {
            CartVO vo = ObjectToVO(cart);
            vos.add(vo);
        });

        return  ResultUtils.success(vos);
    }

    @PostMapping("/update")
    @ApiOperation("更新购物车")
    public BaseResponse<CartItemVO> update(@RequestBody UpdateCartRequestDTO dto, HttpServletRequest request){
        CartFoodItem cartFoodItem = service.update(dto, request);
        if (cartFoodItem== null) return   ResultUtils.success(null);
        CartItemVO vo = modelMapper.map(cartFoodItem, CartItemVO.class);
        return ResultUtils.success(vo);
    }


    private CartVO ObjectToVO(Cart cart){
        //转VO
        List<CartItemVO> itemVOS = new ArrayList<>();
        cart.getCartItems().forEach(cartFoodItem -> {
            FoodVO foodVO = modelMapper.map(cartFoodItem.getFood(), FoodVO.class);
            CartItemVO vo = modelMapper.map(cartFoodItem, CartItemVO.class);
            vo.setFood(foodVO);
            itemVOS.add(vo);
        });
        BusinessVO businessVO = modelMapper.map(businessFeignClient.getById(cart.getBusinessId()), BusinessVO.class);
        CartVO vo = modelMapper.map(cart, CartVO.class);
        vo.setItemVOS(itemVOS);
        vo.setBusinessVO(businessVO);
        return vo;
    }
}
